<?php

class WikiContribsChart {

	var $graph;
	var $bar;
	var $datax;
	var $datay;
	var $article;

	public function __construct( $article, $data ) {
		global $wgJpGraphPath;
		require_once( $wgJpGraphPath . 'jpgraph.php' );
		require_once( $wgJpGraphPath . 'jpgraph_bar.php' );
                $this->datax = $data[0];
                $this->datay = $data[1];
		$this->article = $article;
		$this->createBarChart();
	}

	public function createBarChart() {
               	$this->bar = new BarPlot( $this->datay );
                $this->graph = new Graph( 900, 175, "chart", 60 );
		$this->graph->SetMargin( 60, 20, 30, 50 );
                $this->graph->SetShadow();
                $this->graph->SetScale( 'textlin', 0, 80, 0, 5 );
		$this->graph->xaxis->SetTickLabels( $this->datax );
       	        $this->graph->SetTickDensity( TICKD_SPARSE );
                $this->graph->title->Set( 'Wiki edits - ' . str_replace( '_', ' ', $this->article ) );
       	        $this->graph->Add( $this->bar );
               	$this->graph->Stroke( _IMG_HANDLER );
	}

	public function outputGraph() {
		imagegd2( $this->graph->img->img );
	}

	public function getDataY() {
		return $this->datay;
	}

	public function saveGraph() {
		global $wgJpImgPath;
		$saveTitle = dirname(__FILE__) . '/charts/' .  $this->article . '-bargraph.png';
		imagepng( $this->graph->img->img, $saveTitle );
	}

}
